package multidraw;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;
import java.util.Vector;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import multidraw.model.DrawingCanvasModel;
import multidraw.model.ModelObserver;
import multidraw.model.commands.ChangeColorCommand;
import multidraw.model.commands.ClearCanvasCommand;
import multidraw.model.commands.SwitchingFillingCommand;
import multidraw.model.commands.TransferControlCommand;



public class ClientListPanelController implements ActionListener, ModelObserver {
	private DrawingCanvasModel myModel;
//	private ControlPanelView myView;
	
	private ClientListPanelView clientListPanel;
	
	// TODO add the clientlistpanelview to the initialization methods
	
	public ClientListPanelController(DrawingCanvasModel model, ClientListPanelView view) {
		this.myModel = model;
		this.clientListPanel = view;
		
	}
	
/*
	// I don't believe we have any events to wait on
	 * 
	 * 
	public void itemStateChanged(ItemEvent e) {
		// TODO Auto-generated method stub
		if (e.getStateChange() == ItemEvent.SELECTED) {
			if (myModel.getSelectedObject() != null) {
				myModel.getSelectedObject().setPenColor(itemToColor(e.getItem()));
				//myModel.notifyObservers();
				ChangeColorCommand command = new ChangeColorCommand(myModel.getSelectedObject().getShapeID(),itemToColor(e.getItem()));
				myModel.addCommand(command);
			} else {
		      myModel.setPenColor(itemToColor(e.getItem()));
			
			}			
		}
	}
*/
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub		

		/*
		 * Transfer control event
		 */
		if (e.getSource().equals(clientListPanel.getTransferButton())) {
			JOptionPane.showMessageDialog(null, clientListPanel.getSelectedUser());
		   TransferControlCommand cmd = new TransferControlCommand(clientListPanel.getSelectedUser());
		}
		/*
		 * Revoke event
		 */
		else if (e.getSource().equals(clientListPanel.getRevokeButton()))
			JOptionPane.showMessageDialog(null, "REVOKE!!!");
		{
            
		}	
		
		
		
		/*
		if (e.getSource().equals(myView.getFillButton())) {
			SwitchingFillingCommand command = new SwitchingFillingCommand (myModel.getSelectedObject().getShapeID());
			myModel.addCommand(command);
		
			//myModel.switchSelectedObjectFilling();
		}
		else if (e.getSource().equals(myView.getClearButton()))
			//myModel.clearCanvas();
		{
            ClearCanvasCommand command = new ClearCanvasCommand();
            myModel.addCommand(command);
		}
		else if (e.getSource().equals(myView.getDrawingButton()))
		{
			myView.switchToDrawing();
		}
		else if (e.getSource().equals(myView.getConnectionsButton()))
		{
			myView.switchToConnection();
		}
		else if (e.getSource().equals(myView.getLoginButton())) {
			// pop up a dialog box to get the username
			// have a method like "setUsername()" for the client.
			// i don't think we need any error checking for now.
			
			// within the method in ControlPanelView that this calls,
			// it should also enable the connectButton and the createButton
			// with the "setEnabled(true)" method of the JButton class
			
			
			String loginName = (String) JOptionPane.showInputDialog(myView, "Enter name:",
					"Log in", JOptionPane.PLAIN_MESSAGE, 
					((Icon) new ImageIcon("src/connection.jpg", "Login")), null, null); 
			
			// add logic to check the name first and see if it checks out
			// if it doesn't launch the error box that responds with the appropriate error
			//		-1 name is already in use
			//		-2 name doesn't exist
			
			// for now just should return 0 to work
			if (loginName != null && !loginName.equals("")) {
				myModel.setClientID(loginName);
				try {
					myModel.login();
				} catch (IOException ioe) {
					System.out.println(ioe);
					JOptionPane.showMessageDialog(null, ioe.getMessage());
					return;
				}
				
				myView.getLoginButton().setEnabled(false);
				myView.getCreateButton().setEnabled(true);
				myView.getConnectButton().setEnabled(true);
				clientListPanel = myView.openClientList(loginName);
				
				myModel.notifyObservers();
			}
			
			
		}
		else if (e.getSource().equals(myView.getCreateButton())) {
			// instantiate a new session.
			// OPTIONAL: transfer any existing data to the new session on the server.
				// will need additional logic for requesting a session name
				// unique names will need to be provided on a per user basis
			// set the client as the owner of the current session.
			try {
				myModel.createSession();
			} catch (IOException ioe) {
				JOptionPane.showMessageDialog(null, ioe.getMessage());
			}
		}
		else if (e.getSource().equals(myView.getConnectButton()))
		{
			// This part is not done yet, I was figuring out what the parameters to showInputDialog should be
			String ownerName = (String) JOptionPane.showInputDialog(myView, "Enter Owner name:",
					"Connect to Session", JOptionPane.PLAIN_MESSAGE, 
					((Icon) new ImageIcon("src/connection.jpg", "Connecting")), null, null); 
			
			// if owner name doesn't have a session, pop up an error window. i think there's a joptionpane for that.
			// otherwise, add the client's username and ClientID to the list of active users for that session
			if (ownerName != null && !ownerName.equals("")) {
				try {
					myModel.joinSession(ownerName);
				} catch (IOException ioe) {
					System.out.println(ioe);
				}
				
				clientListPanel.updateUserList(myModel.getClientList());
			}
		}
		
		*/
		
	}
	
	public void updateContent() {
		
	}
}
